clear
set seed [withheld]
set graphics off

local sample youngold
local agegroup "youngold"

local loopnum=${masteriter}

local ltfpvar tfpvar
	quietly use "${data}/data_`agegroup'.dta", clear
	quietly gen Y=1 if  age<=5
	replace Y=0 if Y==.
	quietly cap rename `ltfpvar' lA
	*Groups	
	quietly egen JT=group(J T)
	quietly egen ST=group(S T)
	quietly egen SJT=group(S J T)
	quietly egen tag_SJT=tag(SJT)
	quietly egen tag_ST=tag(ST)

	quietly egen JTY=group(J T Y)
	quietly egen STY=group(S T Y)
	quietly egen SJTY=group(S J T Y)
	quietly egen tag_SJTY=tag(SJTY)
	quietly egen tag_STY=tag(STY)
	
	*Tau weights
	quietly bys SJT: egen emp_SJT=total(empl)
	quietly bys ST: egen emp_ST=total(empl)
	quietly bys JT: egen emp_JT=total(empl)

	quietly bys SJTY: egen emp_SJTY=total(empl)
	quietly bys STY: egen emp_STY=total(empl)
	quietly bys JTY: egen emp_JTY=total(empl)
	
	*Share of industry in total local employment
	quietly gen empshare_SJT=emp_SJT/emp_ST
	quietly gen empshare_SJTY=emp_SJTY/emp_STY
	*quietly gen empindshare_JT=emp_SJT/empind_JT
	
	
	tempfile originaldata`ltfpvar'
	quietly  save `originaldata`ltfpvar''
	
	
local i=1
while(`i'<=`loopnum'){

di "Loop `i'"

	use `originaldata`ltfpvar'', replace
	run "${code}/Youngold_worker.do"
	count if tag_STY & capT_OR1!=. & capT_OR0!=. & capT!=.
		
		*Calculate covariances + coefs
		foreach svar in capT tau{
		tempvar weight
		if("`svar'"=="capT"){
			local tag STY
			gen `weight'=1
			}
			else{
			local tag SJTY
			gen `weight'=empshare_SJT
			}

		
		*FS 
		quietly reg `svar'_OR0 `svar'_OR1 [aw=`weight'] if tag_`tag'==1
		local `svar'FS = _b[`svar'_OR1]
		*RF
		quietly reg `svar' `svar'_OR1 [aw=`weight'] if tag_`tag'==1
		local `svar'RF = _b[`svar'_OR1]
		*IV
		quietly ivreg  `svar' (`svar'_OR0=`svar'_OR1) [aw=`weight'] if tag_`tag'==1
		local `svar'IV= _b[`svar'_OR0]

		*COV
		quietly cor `svar'_OR0 `svar'_OR1  [aw=`weight']  if tag_`tag'==1, cov
		local `svar'halfvar=`r(Var_2)'
		}
		
		keep if tag_STY | tag_SJTY
		keep tau capT tau_OR0 tau_OR1 capT_OR0 capT_OR1 tag_STY tag_SJTY STY SJTY capT_O tau_O empshare_SJT
		gen it=`i'
		if(`i'==1){
		save "${results}/temp/OY_temp_`ltfpvar'_${date}.dta", replace
		}
		else{
		append using "${results}/temp/OY_temp_`ltfpvar'_${date}.dta"
		save "${results}/temp/OY_temp_`ltfpvar'_${date}.dta",replace
		}
		quietly clear
		quietly set obs 1
		
		foreach svar in tau capT{
			foreach var in IV RF FS halfvar{
				 gen `var'_`svar'=``svar'`var''
				}
			}
		quietly gen tfpvar="`ltfpvar'"
		quietly gen iteration=`i'
	
if ("`i'"=="1"){
	quietly save "${results}/temp/results_loop_OY_`sample'_${date}.dta", replace
	}
	else{
	quietly append using "${results}/temp/results_loop_OY_`sample'_${date}.dta"
	quietly save "${results}/temp/results_loop_OY_`sample'_${date}.dta", replace
	}
 
local i=`i'+1
}



foreach svar in capT tau{
tempvar temp1 temp2 temp3 temp4
*Divide data into tfpvar of interest and the rest
*We will only focus on tfvar of interest (=0 for sorting purposes)
 egen `temp1'=mean(RF_`svar')
 egen `temp4'=mean(FS_`svar')
gen `temp2'=abs(`temp1'-RF_`svar')+2*abs(`temp4'-FS_`svar')
sort  `temp2'
local optimalit_`svar'_`ltfpvar'=iteration[1]
}



*Add 95cis
local ub=round(.975*`loopnum')
local lb=max(round(.025*`loopnum'),1)
foreach svar in capT tau{
foreach var in  RF FS IV{
sort tfpvar `var'_`svar'
quietly bys tfpvar: gen ci_ub_`svar'`var'=`var'_`svar'[`ub']
quietly bys tfpvar: gen ci_lb_`svar'`var'=`var'_`svar'[`lb']
}
}
foreach svar in capT tau{
	foreach var in FS RF IV halfvar{
		quietly gen mean_`var'_`svar' =`var'_`svar' 
		}
		}


collapse (first) ci_* (mean) mean_* , by(tfpvar)
quietly save "${results}/temp/results_OY_${date}.dta", replace


	 
*Find and run reg on optimal iteration
local ltfpvar tfpvar
foreach svar in capT tau{
	use "${results}/temp/OY_temp_`ltfpvar'_${date}.dta" if it==`optimalit_`svar'_`ltfpvar''

 	tempvar weight
	if("`svar'"=="tau"){
		local tag SJTY
		gen `weight'=empshare_SJT
		}
	else{
		local tag STY
		gen `weight'=1
		}
		
	*RF	
	quietly binscatter `svar' `svar'_OR1 if tag_`tag' [aw=`weight'], nq(25) savedata("${results}/temp/OY_RF_`svar'_`ltfpvar'_${date}") replace 
	*FS
	quietly binscatter `svar'_OR0 `svar'_OR1 if tag_`tag' [aw=`weight'], nq(25) savedata("${results}/temp/OY_FS_`svar'_`ltfpvar'_${date}") replace 
	

		*OLS
		quietly reg `svar' `svar'_O [aw=`weight'] if tag_`tag'==1, r
		local `svar'OLS_coef = _b[`svar'_O]
		local `svar'OLS_se = _se[`svar'_O]
		
		*FS 
		quietly reg `svar'_OR0 `svar'_OR1 [aw=`weight'] if tag_`tag'==1, r
		local `svar'FS_coef = _b[`svar'_OR1]
		local `svar'FS_se = _se[`svar'_OR1]
		
		*RF
		quietly reg `svar' `svar'_OR1 [aw=`weight'] if tag_`tag'==1, r
		local `svar'RF_coef = _b[`svar'_OR1]
		local `svar'RF_se = _se[`svar'_OR1]
		*IV
		ivreg  `svar' (`svar'_OR0=`svar'_OR1) [aw=`weight'] if tag_`tag'==1, r
		local `svar'IV_coef= _b[`svar'_OR0]
		local `svar'IV_se= _se[`svar'_OR0]
		ivreghdfe  `svar' (`svar'_OR0=`svar'_OR1) [aw=`weight'] if tag_`tag'==1, r 

		*COV
		quietly cor `svar'_OR0 `svar'_OR1  [aw=`weight'] if tag_`tag'==1, cov
		local `svar'halfvar=`r(Var_2)'
		
	clear
	set obs 1
	gen opt_OLS_coef_`svar'= ``svar'OLS_coef'
	gen opt_OLS_se_`svar'= ``svar'OLS_se'	
	gen opt_RF_coef_`svar'= ``svar'RF_coef'
	gen opt_RF_se_`svar'= ``svar'RF_se'
	gen opt_FS_coef_`svar'=``svar'FS_coef'
	gen opt_FS_se_`svar'=``svar'FS_se'
	gen opt_IV_coef_`svar'=``svar'IV_coef'
	gen opt_IV_se_`svar'=``svar'IV_se'
	gen opt_halfvar_`svar'=``svar'halfvar'

	
	quietly merge 1:1 _n using "${results}/temp/results_OY_${date}.dta", nogen
	format ci* mean*  opt* %9.4g
	
	/*This file stores the results found in foonote 30 as well as the slopes and 
	standard errors printed in Figures 5C and 5D*/
	quietly save "${results}/temp/results_OY_${date}.dta", replace
	
	}
	

	

